<template>
  <div class="kw-dialog dialog-mask" v-if="show">
    <div class="dialog-wrapper dialog-drag" ref="dialogWrapper" v-dialog-drag :style="offset">
      <div class="dialog-container">
        <div class="dialog-header">
          <span>{{title}}</span>
          <a class="dialog-close" @click="handlerClose">
            <i class="iconfont icon-close"></i>
          </a>
        </div>
        <div class="dialog-body" :style="bodyOffset">
          <slot></slot>
        </div>
        <div class="dialog-footer">
          <button class="kw-button" @click="handlerCancel">{{cancel}}</button>
          <button class="kw-button kw-button-blue" @click="handlerOk">{{ok}}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'kw-dialog',
    props: {
      value: {
        type: Boolean,
        default: false
      },
      title: {
        type: String
      },
      cancel: {
        type: String,
        default() {
          return '取消'
        }
      },
      ok: {
        type: String,
        default() {
          return '去顶'
        }
      },
      top: {
        default: '30%'
      },
      left: {
        default: '40%'
      },
      width: {
        default: '300px'
      },
      height: {
        default: '200px'
      }
    },
    data() {
      return {
        show: this.value
      }
    },
    directives: {
      'dialog-drag': function (el) {
        const dialogHeaderEl = el.querySelector('.dialog-header')
        const dragDom = el
        dialogHeaderEl.style.cursor = 'move'

        dialogHeaderEl.onmousedown = (e) => {
          const disX = e.clientX - dragDom.offsetLeft
          const disY = e.clientY - dragDom.offsetTop

          document.onmousemove = function (e) {
            let left = e.clientX - disX;
            let top = e.clientY - disY;

            dragDom.style.left = left + 'px';
            dragDom.style.top = top + 'px';
          }

          document.onmouseup = function () {
            document.onmousemove = null
            document.onmouseup = null
          };
        }
      }
    },
    watch: {
      value(val) {
        this.show = val
      }
    },
    computed: {
      offset: function () {
        return {
          top: this.top,
          left: this.left
        }
      },
      bodyOffset: function () {
        return {
          width: this.width,
          height: this.height
        }
      }
    },
    methods: {
      handlerCancel() {
        this.handlerClose()
        this.$emit('on-cancel')
      },
      handlerOk() {
        this.$emit('on-ok')
      },
      handlerClose() {
        this.show = false
        this.$emit('on-close')
      }
    }
  }
</script>

<style lang="less">
  .kw-dialog {
    &.dialog-mask {
      background-color: rgba(0, 0, 0, 0.5);
      position: fixed;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      z-index: 99;
      display: table;
      animation-duration: 0.6s;
      .dialog-wrapper {
        display: table-cell;
        vertical-align: middle;
        animation-duration: 0.3s;
        .dialog-container {
          margin: auto;
          /*top: 50%;*/
          background-color: white;
          /*width: 260px;*/
          /*height: 150px;*/
          border-radius: 6px;
          .dialog-header {
            height: 32px;
            line-height: 32px;
            border-radius: 6px 6px 0 0;
            border-bottom: 1px solid #e8eaec;
            display: flex;
            padding: 0 16px;
            justify-content: left;
            align-items: center;
            font-size: 13px;
            color: #17233d;
            background-color: white;
            .dialog-close {
              z-index: 1;
              font-size: 12px;
              position: absolute;
              right: 8px;
              overflow: hidden;
              cursor: pointer;
            }
          }
          .dialog-body {
            display: flex;
            margin: 5px 0;
            height: 50px;
            font-size: 13px;
            overflow: auto;
          }
          .dialog-footer {
            width: 100%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: #ffffff;
            border-top: 1px solid #e8eaec;
            border-radius: 0 0 6px 6px;
            display: table;
          }
        }
      }
      .dialog-drag {
        position: absolute;
        top: 20%;
        left: 40%;
      }
    }
  }
</style>